package com.inmarry.action;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;

import org.apache.struts.action.*;
import org.apache.struts.util.*;
import com.inmarry.model.*;

import com.inmarry.util.*;

import com.ibatis.sqlmap.client.SqlMapClient;

public final class Active extends Action {
    
    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res ) throws Exception {

        ActionMessages errors = new ActionMessages();

        String activeCode = req.getParameter("code");

        if ( activeCode == null ) {
            errors.add("noActiveCode", new ActionMessage("active.no.activecode"));
        }

        //根据active code取出用户
        SqlMapClient sqlMap = DBConnection.getSqlMapInstance();

        UserBase user = (UserBase)sqlMap.queryForObject("getUserBaseByActiveCode", activeCode);

        if ( user == null ) {
            errors.add("noUserExists", new ActionMessage("active.no.userbase.by.activecode"));
        }

        if ( (user.getIsActive()).equalsIgnoreCase("Y") ) {
            errors.add("userActived", new ActionMessage("active.user.had.been.actived"));
        }

        if ( !errors.isEmpty() ) {
            saveErrors(req, errors);
            return mapping.findForward("activefailed");
        }

        System.err.println("user id" + user.getId());
        System.err.println("is active" + user.getIsActive());

        //激活用户
        sqlMap.update("activeUser", user.getId());


        return mapping.findForward("activeresult");

    }

}


